<template>
  <Dialog class="system-form-disabled" :title="dialogTitle" v-model="dialogVisible">
    <el-form ref="formRef" :model="formData" label-width="100px" v-loading="formLoading">
      <el-form-item label="名字" prop="name">
        <el-input disabled v-model="formData.name" placeholder="请输入名字" />
      </el-form-item>
      <el-form-item label="年龄-test" prop="age">
        <el-input-number
          disabled
          class="!w-240px"
          v-model="formData.age"
          placeholder="请输入年龄"
        />
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-radio-group v-model="formData.sex">
          <el-radio
            disabled
            v-for="dict in getIntDictOptions(DICT_TYPE.SYSTEM_USER_SEX)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="出生年" prop="birthday">
        <el-date-picker
          disabled
          v-model="formData.birthday"
          type="date"
          value-format="x"
          placeholder="选择出生年"
        />
      </el-form-item>
      <el-form-item label="简介" prop="description">
        <Editor disabled v-model="formData.description" height="150px" />
      </el-form-item>
      <el-form-item label="头像" prop="avatar">
        <UploadImg disabled v-model="formData.avatar" />
      </el-form-item>
      <!-- 关联表生成 -->
      <el-form-item label="XXX" prop="xxx">
        <ScorllSelect
          disabled
          v-model="formData.xxx"
          :preMap="formData.xxxMap"
          placeholder="请选择XXX"
          searchKey="xxx"
          :methods="getSelectDataXXX"
          clearable
          filterable
          remote
          class="!w-240px"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import * as Demo01ContactApi from '@/api/infra/demo/demo01'
// 关联表生成-select组件
import * as CodegenApi from '@/api/infra/codegen'
import ScorllSelect from '@/components/ScorllSelect/index.vue'
import { getLabelValueData } from '@/utils/dict'

const { t } = useI18n() // 国际化

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  name: undefined,
  age: undefined,
  sex: undefined,
  birthday: undefined,
  description: undefined,
  avatar: undefined,
  // 关联表生成
  xxx: undefined,
  xxxMap: {
    label: undefined,
    value: undefined
  }
})

const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await Demo01ContactApi.getDemo01Contact(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 重置表单 */
const resetForm = () => {
  Object.keys(formData.value).forEach((key) => {
    formData.value[key] = undefined
  })
  formRef.value?.resetFields()
}

// 关联表生成
const getSelectDataXXX = async (params) => {
  const url = '/system/user/page'
  const mapkey = {
    value: 'id',
    label: 'nickname'
  }
  const res = await CodegenApi.apiSelectPagePubilc(url, params)
  res.list = getLabelValueData(res.list, mapkey)
  return res
}
</script>
